function generateUUID() {
    var d = new Date().getTime();
    var uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
    });
    return UUID(uuid);
};

var id = generateUUID();

use ClientPlatform
var criterias = db.TemplateCriteria.find({CriteriaType: 5})
var criteriaLevel2 = db.CriteriaLevel2.findOne({Name: "Ratings"})

var rootFilterId = generateUUID();
var filterId = generateUUID();
var userId = db.User.findOne({LastName: "Filter"})

var rootFilter = 
{
    "_id" : rootFilterId,
    "Name" : "Standard Filter Test2",
    "Type" : new NumberInt(2),
    "OwnerUserId" : userId._id,
    "LastSaved" : ISODate("2015-01-29T13:12:25.705Z"),
    "FilterId" : filterId,
    "CreatedAt" : ISODate("2015-01-29T12:43:59.700Z"),
    "Version" : new NumberInt(400)
};

var filter = 
{
    "_id" : filterId,
    "CreatedAt" : ISODate("2014-09-09T07:08:00.776Z"),
    "IsCurrent" : false,
    "OwnerUsedId" : userId._id,
    "RootFilterId" : rootFilterId
};
// ADD CRITERIA TYPE SIX 
var typeSixTypes = ['ComparativeGroupAverage','TotalEsgScore','GovernanceScore','SocialScore','EnvironmentScore'];
var typeSixTemplates = db.TemplateCriteria.find({CriteriaType: 6,CriteriaEntityField: {$in: typeSixTypes}});

var filterOrder = 1;
var fullTypeSixCriterias = [];
for (var i = 0 ; i < typeSixTypes.length ; i++)
{
    filterOrder++;
    
    for (var j = 0 ; j < typeSixTemplates.length(); j++)
    {
        if (typeSixTemplates[j].CriteriaEntityField == typeSixTypes[i])
        {
var typeSixCriteria = {
    "_id" : generateUUID(),
    "Name" : typeSixTemplates[j].Name,
    "CriteriaType" : new NumberInt(6),
    "FilterId" : filterId,
    "CriteriaLevel2Id" : criteriaLevel2._id,
    "CompaniesIdsResult" : null,
    "CriteriaEntity" : typeSixTemplates[j].CriteriaEntity,
    "CriteriaEntityField" : typeSixTemplates[j].CriteriaEntityField,
    "Order" : new NumberInt(i),
    "PISymbol" : null,
    "MaxValue" : new NumberInt(101),
    "MinValue" : new NumberInt(0),
    "WeightMatrixId" : new BinData(3,"AAAAAAAAAAAAAAAAAAAAAA==")
};
fullTypeSixCriterias.push(typeSixCriteria);
}
}
}
//
use ClientDW
var templateIndicator = db.IndicatorTemplate.find({},{Number: 1, AnswerCategories: 1});

var fullStandardFilter = [];

for (var i = 0 ; i < criterias.length(); i++)
{
    var answerCategories = [];
    for (var j = 0 ; j < templateIndicator.length(); j++)
    {
        if (templateIndicator[j].Number == criterias[i].TemplateIndicatorNumber)
        {
            answerCategories = templateIndicator[j].AnswerCategories;
        }
    }
var FilterCriteria = {
    "_id" : generateUUID(),
    "Name" : criterias[i].Name,
    "CriteriaType" : new NumberInt(5),
    "FilterId" : filterId,
    "CriteriaLevel2Id" : criteriaLevel2._id,
    "CompaniesIdsResult" : [],
    "CriteriaEntity" : "RPIndicator",
    "CriteriaEntityField" : "SelectedAnswer.Score",
    "Order" : new NumberInt(i + filterOrder),
    "Score" : new NumberInt(0),
    "TemplateIndicatorNumber" : criterias[i].TemplateIndicatorNumber,
    "AnswerCategories" : answerCategories
};

fullStandardFilter.push(FilterCriteria);
}

use ClientPlatform

db.RootFilter.save(rootFilter)
db.Filter.save(filter)

for (var i = 0 ; i < fullStandardFilter.length ; i++)
{
db.FilterCriteria.insert(fullStandardFilter[i])
}

for (var i = 0 ; i < fullTypeSixCriterias.length; i++)
{
    db.FilterCriteria.insert(fullTypeSixCriterias[i])
}